如何将歌曲的状态从停止更改为开始?

发布于 2024-12-08 09:20:46 字数 5033 浏览 0 评论 0原文

我在android中制作了一个应用程序,其中我在xml中采用了三个按钮,在java中我设置了onclick事件,

当单击播放按钮时,歌曲已准备好并播放, 单击停止按钮时歌曲停止, 单击暂停按钮时,歌曲暂停。

但我的问题是,当我单击播放按钮时,在单击停止按钮后播放歌曲,然后在单击播放按钮后停止播放歌曲,但此时未播放歌曲,这是

我为此应用程序创建的代码。下面给出。

package com.mydemo;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MydemoActivity extends Activity {
/** Called when the activity is first created. */

Button bPlay,bPause,bStop;
MediaPlayer mp;
int position = 0;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mp = MediaPlayer.create(this, R.raw.fluet);

    bPlay = (Button) findViewById(R.id.bPlay);
    bPause = (Button)findViewById(R.id.bPause);
    bStop = (Button) findViewById(R.id.bStop);

    bPause.setVisibility(View.GONE);

    mp.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer arg0) {

            position = 0;
            bPlay.setVisibility(View.VISIBLE);
            bPause.setVisibility(View.GONE);

        }
    });

    bPlay.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            if(position > 0)
            {
                mp.seekTo(position);
                mp.start();
            }

            try {
                mp.prepare();
            } catch (IllegalStateException e)     {                 
                e.printStackTrace();
            } catch (IOException e) {                   
                e.printStackTrace();
            }
            mp.start();

            bPlay.setVisibility(View.GONE);
            bPause.setVisibility(View.VISIBLE);
        }
    });

    bPause.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {            

            if(mp!=null)
            {
                mp.pause();
                position = mp.getCurrentPosition();
            }

            bPlay.setVisibility(View.VISIBLE);
            bPause.setVisibility(View.GONE);
        }
    });        

    bStop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            mp.stop();
            position = 0;

            bPlay.setVisibility(View.VISIBLE);
                bPause.setVisibility(View.GONE);                
        }
    });
  }
}

请参阅此更新的代码:

package com.mydemo;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MydemoActivity extends Activity {
/** Called when the activity is first created. */

Button bPlay,bPause,bStop;
MediaPlayer mp;
int position = 0;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mp = MediaPlayer.create(this, R.raw.fluet);

    bPlay = (Button) findViewById(R.id.bPlay);
    bPause = (Button)findViewById(R.id.bPause);
    bStop = (Button) findViewById(R.id.bStop);

    bPause.setVisibility(View.GONE);

    mp.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer arg0) {

            position = 0;
            bPlay.setVisibility(View.VISIBLE);
            bPause.setVisibility(View.GONE);

        }
    });

    bPlay.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {



            if(mp.isPlaying() == false)
            {
                try {
                    mp.prepare();
                } catch (IllegalStateException e) {                 
                    e.printStackTrace();
                } catch (IOException e) {                   
                    e.printStackTrace();
                }
                mp.start();
            }


            bPlay.setVisibility(View.GONE);
            bPause.setVisibility(View.VISIBLE);
        }
    });

    bPause.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {            

            if(mp!=null)
            {
                mp.pause();
                position = mp.getCurrentPosition();
            }

            bPlay.setVisibility(View.VISIBLE);
            bPause.setVisibility(View.GONE);
        }
    });        

    bStop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            mp.stop();
            position = 0;

            bPlay.setVisibility(View.VISIBLE);
             bPause.setVisibility(View.GONE);               
        }
    });
 }
}

I have make one application in android in which i take three buttons in xml and in java i set the onclick event,

when play button is clicked song is prepared and play,
when stop button is clicked song is stopped,
when pause button is clicked song is paused.

But my problem is that when I clicked play button song is played after i click on stop button then song is stopped after I clicked on play button, but at this time song is not played,

the code I created for this app. is given below.

package com.mydemo;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MydemoActivity extends Activity {
/** Called when the activity is first created. */

Button bPlay,bPause,bStop;
MediaPlayer mp;
int position = 0;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mp = MediaPlayer.create(this, R.raw.fluet);

    bPlay = (Button) findViewById(R.id.bPlay);
    bPause = (Button)findViewById(R.id.bPause);
    bStop = (Button) findViewById(R.id.bStop);

    bPause.setVisibility(View.GONE);

    mp.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer arg0) {

            position = 0;
            bPlay.setVisibility(View.VISIBLE);
            bPause.setVisibility(View.GONE);

        }
    });

    bPlay.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            if(position > 0)
            {
                mp.seekTo(position);
                mp.start();
            }

            try {
                mp.prepare();
            } catch (IllegalStateException e)     {                 
                e.printStackTrace();
            } catch (IOException e) {                   
                e.printStackTrace();
            }
            mp.start();

            bPlay.setVisibility(View.GONE);
            bPause.setVisibility(View.VISIBLE);
        }
    });

    bPause.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {            

            if(mp!=null)
            {
                mp.pause();
                position = mp.getCurrentPosition();
            }

            bPlay.setVisibility(View.VISIBLE);
            bPause.setVisibility(View.GONE);
        }
    });        

    bStop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            mp.stop();
            position = 0;

            bPlay.setVisibility(View.VISIBLE);
                bPause.setVisibility(View.GONE);                
        }
    });
  }
}

see this Updated code:

package com.mydemo;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MydemoActivity extends Activity {
/** Called when the activity is first created. */

Button bPlay,bPause,bStop;
MediaPlayer mp;
int position = 0;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mp = MediaPlayer.create(this, R.raw.fluet);

    bPlay = (Button) findViewById(R.id.bPlay);
    bPause = (Button)findViewById(R.id.bPause);
    bStop = (Button) findViewById(R.id.bStop);

    bPause.setVisibility(View.GONE);

    mp.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer arg0) {

            position = 0;
            bPlay.setVisibility(View.VISIBLE);
            bPause.setVisibility(View.GONE);

        }
    });

    bPlay.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {



            if(mp.isPlaying() == false)
            {
                try {
                    mp.prepare();
                } catch (IllegalStateException e) {                 
                    e.printStackTrace();
                } catch (IOException e) {                   
                    e.printStackTrace();
                }
                mp.start();
            }


            bPlay.setVisibility(View.GONE);
            bPause.setVisibility(View.VISIBLE);
        }
    });

    bPause.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {            

            if(mp!=null)
            {
                mp.pause();
                position = mp.getCurrentPosition();
            }

            bPlay.setVisibility(View.VISIBLE);
            bPause.setVisibility(View.GONE);
        }
    });        

    bStop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            mp.stop();
            position = 0;

            bPlay.setVisibility(View.VISIBLE);
             bPause.setVisibility(View.GONE);               
        }
    });
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

浅暮の光 2024-12-15 09:20:46

编辑::
您需要在播放事件中应用条件,例如 ::

if(mp.isplaying == false)
{
   //play the song
}

Edit ::
you need to apply condition in play event like ::

if(mp.isplaying == false)
{
   //play the song
}
樱花细雨 2024-12-15 09:20:46
 bStop.setOnClickListener(new View.OnClickListener() 
        {

        @Override
        public void onClick(View arg0) {

            mp.release();
            position = 0;

            bPlay.setVisibility(View.VISIBLE);
             bPause.setVisibility(View.GONE);               
        }
    });
bStop.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {

       if(mp.isplaying == false)
        {
   mp = MediaPlayer.create(this, R.raw.fluet);

        }
    }
});
 bStop.setOnClickListener(new View.OnClickListener() 
        {

        @Override
        public void onClick(View arg0) {

            mp.release();
            position = 0;

            bPlay.setVisibility(View.VISIBLE);
             bPause.setVisibility(View.GONE);               
        }
    });
bStop.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {

       if(mp.isplaying == false)
        {
   mp = MediaPlayer.create(this, R.raw.fluet);

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