1

我正在尝试通过另一个 java 类从 java 类访问方法。我已经创建了该类的对象并将该类导入我的 java 类,但我无法访问我的代码。

   package com.example.musicplayer;

import java.io.File;
import java.util.ArrayList;
import java.util.List;



import android.R.string;

import android.app.ListActivity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.provider.MediaStore.Audio.Playlists;
import android.test.suitebuilder.TestSuiteBuilder.FailedToCreateTests;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


        public class MainActivity extends ListActivity {

    public static final String MEDIA_PATH = new String("/sdcard/");
    private List<String> songs = new ArrayList<String>();
    public MediaPlayer mp = new MediaPlayer();
    private int currentPosition = 0;

    public MediaPlayer getmp(){
        return mp;
    }
    public void setmp(MediaPlayer mp){
        this.mp = mp;
    }

       @Override
          protected void onCreate(Bundle icicle) {
             super.onCreate(icicle);
           setContentView(R.layout.activity_main);
         updateSongList();         
     }

            private void updateSongList() {
        // TODO Auto-generated method stub
        File home = new File(MEDIA_PATH);
        if(home.listFiles(new Mp3Filter()).length > 0){
            for(File file: home.listFiles(new Mp3Filter())){
                songs.add(file.getName());
            }
          }
                ArrayAdapter<String> songList = new ArrayAdapter<String>(this, R.layout.song_item, songs);


        setListAdapter(songList);
      }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id){
        currentPosition = position;
        playSong(MEDIA_PATH + songs.get(position));

        //Toast.makeText(getApplicationContext(), songs.get(position).toString(), Toast.LENGTH_SHORT).show();

        final String songName = songs.get(position).toString();
        //final TextView textchange = (TextView) v.findViewById(R.id.current_song_name);

        Intent in = new Intent(this, current_song.class);
        in.putExtra("song_name", songName);
        startActivity(in);

        //Toast.makeText(getApplicationContext(), "End of Song List", Toast.LENGTH_SHORT).show();       
        //textchange.setText(songName);
    }


    private void playSong(String songPath) {
        // TODO Auto-generated method stub

        try{

            mp.reset();
            mp.setDataSource(songPath);
            mp.prepare();
            mp.start();

            mp.setOnCompletionListener(new OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    // TODO Auto-generated method stub
                    auto_nextSong();
                }               
            });

        }
        catch (Exception e) {
            // TODO: handle exception
        }

    }

    public void pause(){
        mp.pause();
    }

    public void stop(){

        mp.stop();
    }

    public void next(){ 
        Toast.makeText(getApplicationContext(), "In Next()", Toast.LENGTH_SHORT).show();
        if(currentPosition < songs.size()){
            currentPosition = currentPosition + 1;
            playSong(MEDIA_PATH + songs.get(currentPosition));
        }
    }

    public void prv(){
        if(currentPosition > songs.size()){
            currentPosition = currentPosition - 1;      
            playSong(MEDIA_PATH + songs.get(currentPosition));
        }
    }

    private void auto_nextSong() {
        // TODO Auto-generated method stub
        if(++currentPosition >= songs.size()){
            currentPosition = 0;
            Toast.makeText(getApplicationContext(), "End of Song List", Toast.LENGTH_SHORT).show();
        }
        else{
            playSong(MEDIA_PATH + songs.get(currentPosition));
            Toast.makeText(getApplicationContext(), "Next Song", Toast.LENGTH_SHORT).show();
        }
    }

     }

从上面的类我试图访问 pause() 和 stop()

这是我要访问的课程

package com.example.musicplayer;



     import com.example.musicplayer.MainActivity;
             import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

        public class current_song extends Activity implements OnClickListener{


     public MainActivity ma;
     protected void onCreate(Bundle icicle, MainActivity ma) {

         Bundle extra = getIntent().getExtras();

         super.onCreate(icicle);
         setContentView(R.layout.songplay_page);

         if(extra != null){
             String song_name = extra.getString("song_name");
             TextView textchange = (TextView)findViewById(R.id.current_song_name);
             textchange.setText(song_name);
             textchange.setSelected(true);
         }       

         //MainActivity ma = ((MainActivity)getApplicationContext());
         //MediaPlayer mp = ma.getmp();

         this.ma = ma;
         Button btn_pause = (Button)findViewById(R.id.pause_btn);
         btn_pause.setOnClickListener(this);    

         Button btn_next = (Button)findViewById(R.id.next_btn);
         btn_next.setOnClickListener(this);

         Button btn_prv = (Button)findViewById(R.id.prv_btn);
         btn_prv.setOnClickListener(this);

         Button btn_stop = (Button)findViewById(R.id.stop_btn);
         btn_stop.setOnClickListener(this);

     }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        //Toast.makeText(getApplicationContext(), "In Onclick ()", Toast.LENGTH_SHORT).show();
        switch(v.getId())
        {
            case R.id.pause_btn:
                ((MainActivity)ma).pause();
            break;

            case R.id.next_btn:
                //ma.next();
            break;

            case R.id.prv_btn:
                //ma.prv();
            break;

            case R.id.stop_btn:
                ((MainActivity)ma).stop();
            break;
        }

    }

       }

这个概念是我想停下来,暂停正在播放的音乐。如果我的逻辑我错了,请指导我如何去做,否则请帮助我如何在 Current music java 类中控制我当前的音乐。

4

2 回答 2

2

请把你所有的音乐播放代码移到服务中。活动不是这样工作的。

考虑以下内容: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle http://developer.android.com/reference/android/app/Service.html

于 2013-02-07T16:51:53.353 回答
0

我认为你是 android 新手,但没问题:你不能像这样从任何其他类调用活动的函数(Object.method();)

所以对于这种类型的处理,您应该创建一个inner Handler classwhichextends Handler inside your MainActivity.并覆盖它的 onHandle(message msg){} 方法。

When You start current_song activity pass this handler object in the intent.

当您想从 MainActivity 执行任何任务时(例如停止、暂停),pass messages through that handler例如object.sendmessage(new msgObject("stop"));

inside you onHandle() method call your stop or pause methods through diffrentiating command received from the message object.

喜欢if(message's content == stop) MainActivity.this.stop();

于 2013-02-07T17:07:15.527 回答