在 Android 上使用 MediaPlayer 类播放 mp3 问题
我的代码有什么问题吗?我有一个切换按钮,我想播放/停止 mp3。我想代码应该如下:
package com.android.iFocus;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ToggleButton;
public class iFocusActivity extends Activity implements OnClickListener {
public int count;
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.rain);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ToggleButton toggleRain = (ToggleButton)findViewById(R.id.toggleRain);
//Define Listeners
toggleRain.setOnClickListener(this);
count = 0;
}
@Override
public void onClick(View toggleRain) {
if(count==0){
mediaPlayer.start();
count=1;
} else {
//MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.rain);
mediaPlayer.pause();
mediaPlayer.stop();
mediaPlayer.release();
count=0;
}
}
}
问题是:Eclipse 没有给出任何错误,但在模拟器/手机上它给了我一个异常并在启动后立即死掉。这里是:
10-02 20:28:24.312: INFO/ActivityManager(59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.android.iFocus/.iFocusActivity }
10-02 20:28:24.392: DEBUG/AndroidRuntime(960): Shutting down VM
10-02 20:28:24.402: DEBUG/dalvikvm(960): Debugger has detached; object registry had 1 entries
10-02 20:28:24.462: INFO/ActivityManager(59): Start proc com.android.iFocus for activity com.android.iFocus/.iFocusActivity: pid=967 uid=10036 gids={}
10-02 20:28:24.502: INFO/AndroidRuntime(960): NOTE: attach of thread 'Binder Thread #3' failed
10-02 20:28:25.822: DEBUG/AndroidRuntime(967): Shutting down VM
10-02 20:28:25.822: WARN/dalvikvm(967): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-02 20:28:25.932: ERROR/AndroidRuntime(967): FATAL EXCEPTION: main
10-02 20:28:25.932: ERROR/AndroidRuntime(967): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.android.iFocus/com.android.iFocus.iFocusActivity}: java.lang.NullPointerException
嗯,当我在 onClick 内部类中初始化 mediaPlayer 时,它不会给我任何错误,并且应用程序不会给我任何启动歌曲的错误。但它并没有按应有的方式停止。因此,当我单击切换按钮时,它会启动,当我再次单击时,它不会执行任何操作,只会在日志猫上给我一个错误:
第一次按下切换按钮时出错并且歌曲开始正常(但给出此错误):
10-02 20:39:02.712: INFO/ActivityManager(59): Start proc com.android.iFocus for activity com.android.iFocus/.iFocusActivity: pid=996 uid=10036 gids={}
10-02 20:39:02.782: INFO/AndroidRuntime(989): NOTE: attach of thread 'Binder Thread #3' failed
10-02 20:39:04.432: INFO/ActivityManager(59): Displayed activity com.android.iFocus/.iFocusActivity: 1804 ms (total 640049 ms)
10-02 20:39:08.672: DEBUG/AudioSink(34): bufferCount (4) is too small and increased to 12
10-02 20:39:08.982: WARN/AudioFlinger(34): write blocked for 73 msecs, 2105 delayed writes, thread 0xb3b8
10-02 20:39:09.682: DEBUG/dalvikvm(437): GC_EXPLICIT freed 686 objects / 38192 bytes in 216ms
10-02 20:39:14.502: WARN/AudioFlinger(34): write blocked for 86 msecs, 2110 delayed writes, thread 0xb3b8
10-02 20:39:14.642: DEBUG/dalvikvm(188): GC_EXPLICIT freed 164 objects / 11408 bytes in 176ms
10-02 20:39:19.622: DEBUG/dalvikvm(261): GC_EXPLICIT freed 43 objects / 1912 bytes in 154ms
10-02 20:39:20.352: WARN/AudioFlinger(34): write blocked for 78 msecs, 2119 delayed writes, thread 0xb3b8
错误当我再次按下切换按钮时,歌曲应该停止:
10-02 20:43:22.412: ERROR/MediaPlayer(1032): pause called in state 8
10-02 20:43:22.412: ERROR/MediaPlayer(1032): error (-38, 0)
10-02 20:43:22.412: ERROR/MediaPlayer(1032): stop called in state 0
10-02 20:43:22.412: ERROR/MediaPlayer(1032): error (-38, 0)
10-02 20:43:22.612: WARN/MediaPlayer(1032): mediaplayer went away with unhandled events
10-02 20:43:22.612: WARN/MediaPlayer(1032): mediaplayer went away with unhandled events
10-02 20:43:22.622: WARN/MediaPlayer(1032): mediaplayer went away with unhandled events
10-02 20:43:22.622: WARN/MediaPlayer(1032): mediaplayer went away with unhandled events
What is wrong with my code? I have a toggle button and i would like to play/stop an mp3. I guess that the code should be as follows:
package com.android.iFocus;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ToggleButton;
public class iFocusActivity extends Activity implements OnClickListener {
public int count;
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.rain);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ToggleButton toggleRain = (ToggleButton)findViewById(R.id.toggleRain);
//Define Listeners
toggleRain.setOnClickListener(this);
count = 0;
}
@Override
public void onClick(View toggleRain) {
if(count==0){
mediaPlayer.start();
count=1;
} else {
//MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.rain);
mediaPlayer.pause();
mediaPlayer.stop();
mediaPlayer.release();
count=0;
}
}
}
the problem is: Eclipse doesn't give any error, but on emulator/phone it gives me an exception and die immediately after started. here goes:
10-02 20:28:24.312: INFO/ActivityManager(59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.android.iFocus/.iFocusActivity }
10-02 20:28:24.392: DEBUG/AndroidRuntime(960): Shutting down VM
10-02 20:28:24.402: DEBUG/dalvikvm(960): Debugger has detached; object registry had 1 entries
10-02 20:28:24.462: INFO/ActivityManager(59): Start proc com.android.iFocus for activity com.android.iFocus/.iFocusActivity: pid=967 uid=10036 gids={}
10-02 20:28:24.502: INFO/AndroidRuntime(960): NOTE: attach of thread 'Binder Thread #3' failed
10-02 20:28:25.822: DEBUG/AndroidRuntime(967): Shutting down VM
10-02 20:28:25.822: WARN/dalvikvm(967): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-02 20:28:25.932: ERROR/AndroidRuntime(967): FATAL EXCEPTION: main
10-02 20:28:25.932: ERROR/AndroidRuntime(967): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.android.iFocus/com.android.iFocus.iFocusActivity}: java.lang.NullPointerException
Well, when i initialize mediaPlayer inside onClick inner class, it doesn't give me any error and the aplication doesn't give me any error for to start the song. but it does not stop as should. So, when i click on toggleButton, it starts, when i click again, it doesn't do anything but give me an error on log cat:
Error when first press toggle button and the song starts ok (but give this error):
10-02 20:39:02.712: INFO/ActivityManager(59): Start proc com.android.iFocus for activity com.android.iFocus/.iFocusActivity: pid=996 uid=10036 gids={}
10-02 20:39:02.782: INFO/AndroidRuntime(989): NOTE: attach of thread 'Binder Thread #3' failed
10-02 20:39:04.432: INFO/ActivityManager(59): Displayed activity com.android.iFocus/.iFocusActivity: 1804 ms (total 640049 ms)
10-02 20:39:08.672: DEBUG/AudioSink(34): bufferCount (4) is too small and increased to 12
10-02 20:39:08.982: WARN/AudioFlinger(34): write blocked for 73 msecs, 2105 delayed writes, thread 0xb3b8
10-02 20:39:09.682: DEBUG/dalvikvm(437): GC_EXPLICIT freed 686 objects / 38192 bytes in 216ms
10-02 20:39:14.502: WARN/AudioFlinger(34): write blocked for 86 msecs, 2110 delayed writes, thread 0xb3b8
10-02 20:39:14.642: DEBUG/dalvikvm(188): GC_EXPLICIT freed 164 objects / 11408 bytes in 176ms
10-02 20:39:19.622: DEBUG/dalvikvm(261): GC_EXPLICIT freed 43 objects / 1912 bytes in 154ms
10-02 20:39:20.352: WARN/AudioFlinger(34): write blocked for 78 msecs, 2119 delayed writes, thread 0xb3b8
Error when i again press the toggleButton and the song should stop:
10-02 20:43:22.412: ERROR/MediaPlayer(1032): pause called in state 8
10-02 20:43:22.412: ERROR/MediaPlayer(1032): error (-38, 0)
10-02 20:43:22.412: ERROR/MediaPlayer(1032): stop called in state 0
10-02 20:43:22.412: ERROR/MediaPlayer(1032): error (-38, 0)
10-02 20:43:22.612: WARN/MediaPlayer(1032): mediaplayer went away with unhandled events
10-02 20:43:22.612: WARN/MediaPlayer(1032): mediaplayer went away with unhandled events
10-02 20:43:22.622: WARN/MediaPlayer(1032): mediaplayer went away with unhandled events
10-02 20:43:22.622: WARN/MediaPlayer(1032): mediaplayer went away with unhandled events
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,我的分析:
1. 你没有在 onCreate():
'this' <--- this thing is NULL 中初始化 MediaPlayer,所以你在运行时遇到了 NullPointerException,首先加载应用程序计时。
2。第二次单击按钮时,您调用了
下次单击时,MediaPlayer State 出现异常
好吧,解决方法非常简单,您需要考虑 Android 编程的最佳实践:
当然,它的工作原理就像魅力 ^^!
有很多方法可以改进这个简单的应用程序,但是,我想您可以通过查看 Android 开发人员参考文档来找到答案:)
First thing first, my analysis:
1. You didn't init the MediaPlayer inside onCreate():
'this' <--- this thing is NULL, so you've got a NullPointerException at Runtime, first loading app timing.
2. On second click to button, you've called
And next time you click, exception at MediaPlayer State
Well, the fix is pretty much simple, you need to consider best practice on Android programming:
Certainly it works like charm ^^!
There are many ways to improve this simple app, however, I guess you can find out by looking over Android Developers' References Documentation :)
您是否尝试过将初始化移到 onCreate 内部而不是仅移到类主体内部?这将是最好的地方。
如果您在 onClick 内部进行初始化,则您显示的错误是预期的。这是因为每次单击时都会创建一个新的 MediaPlayer 实例。
Have you tried moving the initialization inside of onCreate instead of just inside the class body? This would be the best place to do it.
If you initialize inside of onClick, the error you show is expected. This is because a new MediaPlayer instance is being created every time you click.
您的问题出在此处的
release()
语句上:您可以通过几种不同的方法来更改此设置,具体取决于您想要的结果。如果您只想播放/暂停,正如您所说,那么您只需删除
stop()
和release()
调用即可。特别是release()
。该调用将音频资源释放回系统,这意味着您需要将其恢复到准备状态,然后才能再次使用它。我强烈建议您非常彻底地阅读此参考文档。 MediaPlayer 类相当复杂,当状态管理不当时,很容易出现这样的错误,而且很常见。
Your problem is with the
release()
statement here:There are a few different ways you can change this, depending on your desired result. If you just want to play/pause, as you've said, then you only need to remove the
stop()
andrelease()
calls. ESPECIALLYrelease()
. That call releases the audio resources back to the system, meaning you'll need to get it back into a prepared state before you can use it again.I highly recommend reading this reference document VERY thoroughly. The MediaPlayer class is fairly complex, and it's easy and common for mistakes like this to appear when the states aren't managed properly.