简体   繁体   English

在Android应用中播放媒体播放器

[英]Playing Media player in Android app

I am creating a media player with start, pause, stop buttons. 我正在创建一个带有开始,暂停,停止按钮的媒体播放器。 Start and stop buttons work fine, the problem is with the pause button. 启动和停止按钮可以正常工作,问题出在暂停按钮上。

When no audio files are playing (and also not at all paused any audio), if I click on the pause button, it throws this error message: 当没有音频文件正在播放时(也根本没有暂停任何音频),如果我单击暂停按钮,则会抛出此错误消息:

Unfortunately App has stopped 不幸的是,应用程序已停止

What is the problem? 问题是什么?

public class activity_12 extends Activity {

    MediaPlayer sound;
    int paused;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_12);
    }
    public void Play(View view) {
        if(sound==null) {
            sound=MediaPlayer.create(this,R.raw.fil);
            sound.start();
        }else if(!sound.isPlaying()) {
            sound.seekTo(paused);
            sound.start();
        }
    }
    public void Pause(View view) {
        sound.pause();
        paused=sound.getCurrentPosition();
    }
    public void Stop(View view) {
        sound.release();
        sound=null;
    }
}

You are facing the problem because you are not declaring the functions properly.Please check this code: 您正面临问题,因为您没有正确声明功能。请检查以下代码:

Define your XML like this: 像这样定义您的XML:

<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:paddingBottom="@dimen/activity_vertical_margin"  
    android:paddingLeft="@dimen/activity_horizontal_margin"  
    android:paddingRight="@dimen/activity_horizontal_margin"  
    android:paddingTop="@dimen/activity_vertical_margin"  
    tools:context=".MainActivity" >  
  <Button  
        android:id="@+id/button1"  
        style="?android:attr/buttonStyleSmall"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignLeft="@+id/textView1"  
        android:layout_below="@+id/textView1"  
        android:layout_marginTop="48dp"  
        android:text="start" />  

    <Button  
        android:id="@+id/button2"  
        style="?android:attr/buttonStyleSmall"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignTop="@+id/button1"  
        android:layout_toRightOf="@+id/button1"  
        android:text="pause" />  

    <Button  
        android:id="@+id/button3"  
        style="?android:attr/buttonStyleSmall"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignTop="@+id/button2"  
        android:layout_toRightOf="@+id/button2"  
        android:text="stop" />  

</RelativeLayout>

You should define your Activity like this: 您应该这样定义您的活动:

import android.media.MediaPlayer;  
import android.os.Bundle;  
import android.os.Environment;  
import android.app.Activity;  
import android.view.Menu;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  

public class MainActivity extends Activity {  
    Button start,pause,stop;  
    MediaPlayer mediaPlayer;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  

        start=(Button)findViewById(R.id.button1);  
        pause=(Button)findViewById(R.id.button2);  
        stop=(Button)findViewById(R.id.button3);  
        //creating media player  
        mediaPlayer= new MediaPlayer();



        start.setOnClickListener(new OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                mediaPlayer.start();  
            }  
        });  
        pause.setOnClickListener(new OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                mediaPlayer.pause();  
            }  
        });  
        stop.setOnClickListener(new OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                mediaPlayer.stop();  
            }  
        });  
    }  
}  

For reference, MediaPlayer's states are very particular. 作为参考,MediaPlayer的状态非常特殊。 If you call a state checking function like isPlaying() at the wrong time or in your case .pause() at the wrong time, it will automatically transition to an error state. 如果您在错误的时间调用了一个状态检查函数,例如isPlaying() ,或者在错误的时间调用了.pause() ,它将自动转换为错误状态。 You need to track the state closely yourself. 您需要自己密切跟踪状态。

Check out the MediaPlayer docs, in particular the State Diagram 查看MediaPlayer文档,尤其是状态图

And for pause it says in regard to it's relevant states in calling the pause() function: 对于暂停,它在调用pause()函数时说明了其相关状态:

Successful invoke of this method in a valid state transfers the object to the Paused state. 在有效状态下成功调用此方法会将对象转移到“暂停”状态。 Calling this method in an invalid state transfers the object to the Error state. 在无效状态下调用此方法会将对象转移到错误状态。

An Error state with MediaPlayer invokes a crash. MediaPlayer的错误状态会引发崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM