在 Android 上使用 MediaPlayer 类播放 mp3 问题

发布于 2024-12-07 20:20:37 字数 4313 浏览 0 评论 0原文

我的代码有什么问题吗?我有一个切换按钮,我想播放/停止 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不疑不惑不回忆 2024-12-14 20:20:37

首先,我的分析:
1. 你没有在 onCreate():

MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.rain);

'this' <--- this thing is NULL 中初始化 MediaPlayer,所以你在运行时遇到了 NullPointerException,首先加载应用程序计时。

2。第二次单击按钮时,您调用了

mediaPlayer.release();

下次单击时,MediaPlayer State 出现异常

好吧,解决方法非常简单,您需要考虑 Android 编程的最佳实践:

package pete.android.study;

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 Main extends Activity implements OnClickListener {
        // declare controls
        public int count = 0;
        MediaPlayer mediaPlayer = null;  
        ToggleButton toggleRain = null;
        /*
         * (non-Javadoc)
         * @see android.app.Activity#onCreate(android.os.Bundle)
         */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            // load layout
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            // load controls
            toggleRain = (ToggleButton)findViewById(R.id.toggleRain);
            // init player
            mediaPlayer = MediaPlayer.create(this, R.raw.rain);
            // set click event handler
            toggleRain.setOnClickListener(this);
            // init state for playing
            count = 0;
        }

        /*
         * (non-Javadoc)
         * @see android.view.View.OnClickListener#onClick(android.view.View)
         */
        @Override    
        public void onClick(View toggleRain) {
            if(count == 0){
                mediaPlayer.start();
                count = 1;
            } else {
                mediaPlayer.pause();                
                count = 0;
            }
        }

        /*
         * (non-Javadoc)
         * @see android.app.Activity#onDestroy()
         */
        @Override
        protected void onDestroy() {
            if(mediaPlayer != null) {
                mediaPlayer.stop();
                mediaPlayer.release();
                mediaPlayer = null;
            }
        }

}

当然,它的工作原理就像魅力 ^^!
有很多方法可以改进这个简单的应用程序,但是,我想您可以通过查看 Android 开发人员参考文档来找到答案:)

First thing first, my analysis:
1. You didn't init the MediaPlayer inside onCreate():

MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.rain);

'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

mediaPlayer.release();

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:

package pete.android.study;

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 Main extends Activity implements OnClickListener {
        // declare controls
        public int count = 0;
        MediaPlayer mediaPlayer = null;  
        ToggleButton toggleRain = null;
        /*
         * (non-Javadoc)
         * @see android.app.Activity#onCreate(android.os.Bundle)
         */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            // load layout
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            // load controls
            toggleRain = (ToggleButton)findViewById(R.id.toggleRain);
            // init player
            mediaPlayer = MediaPlayer.create(this, R.raw.rain);
            // set click event handler
            toggleRain.setOnClickListener(this);
            // init state for playing
            count = 0;
        }

        /*
         * (non-Javadoc)
         * @see android.view.View.OnClickListener#onClick(android.view.View)
         */
        @Override    
        public void onClick(View toggleRain) {
            if(count == 0){
                mediaPlayer.start();
                count = 1;
            } else {
                mediaPlayer.pause();                
                count = 0;
            }
        }

        /*
         * (non-Javadoc)
         * @see android.app.Activity#onDestroy()
         */
        @Override
        protected void onDestroy() {
            if(mediaPlayer != null) {
                mediaPlayer.stop();
                mediaPlayer.release();
                mediaPlayer = null;
            }
        }

}

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 :)

私野 2024-12-14 20:20:37

您是否尝试过将初始化移到 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.

清风不识月 2024-12-14 20:20:37

您的问题出在此处的 release() 语句上:

if(count==0){
    mediaPlayer.start();
    count = 1;
} else {
    //MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.rain);
    mediaPlayer.pause();
    mediaPlayer.stop();
    mediaPlayer.release();
    count = 0;
}

您可以通过几种不同的方法来更改此设置,具体取决于您想要的结果。如果您只想播放/暂停,正如您所说,那么您只需删除 stop()release() 调用即可。特别是release()。该调用将音频资源释放回系统,这意味着您需要将其恢复到准备状态,然后才能再次使用它。

我强烈建议您非常彻底地阅读此参考文档。 MediaPlayer 类相当复杂,当状态管理不当时,很容易出现这样的错误,而且很常见。

Your problem is with the release() statement here:

if(count==0){
    mediaPlayer.start();
    count = 1;
} else {
    //MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.rain);
    mediaPlayer.pause();
    mediaPlayer.stop();
    mediaPlayer.release();
    count = 0;
}

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() and release() calls. ESPECIALLY release(). 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文