当我们在android中自动交换屏幕时,如何在后台播放音频

时间:2013-03-06 05:50:41

标签: android

在我的项目中,我使用片段进行swape屏幕,当新屏幕出现时自动播放背景音频。怎么能实现这个帮助...

public class Main extends FragmentActivity{

    private PagerAdapter mPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.view);

        this.initialisePaging();
    }

    private void initialisePaging() 
    {

        List<Fragment> fragments = new Vector<Fragment>();

        fragments.add(Fragment.instantiate(this, Tab1Fragment1.class.getName()));
        fragments.add(Fragment.instantiate(this, Tab1Fragment2.class.getName()));
        fragments.add(Fragment.instantiate(this, Tab1Fragment3.class.getName()));
        fragments.add(Fragment.instantiate(this, Tab1Fragment4.class.getName()));
        fragments.add(Fragment.instantiate(this, Tab1Fragment5.class.getName()));
        fragments.add(Fragment.instantiate(this, Tab1Fragment6.class.getName()));
        this.mPagerAdapter  = new PagerAdapter(super.getSupportFragmentManager(), fragments);


        ViewPager pager = (ViewPager)super.findViewById(R.id.viewpager);
        pager.setAdapter(this.mPagerAdapter);
    }
}

这是pagerview ....

package com.example.swape;
import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;


public class PagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments;

    public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int position) {
        return this.fragments.get(position);
    }


    @Override
    public int getCount() {
        return this.fragments.size();
    }
}

这是在背景中显示带文字和播放音频的图片的片段画面....

package com.example.swape;



import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;


//import com.andy.R;


public class Tab1Fragment1 extends Fragment {

    String str = "DEATH WARENT";    
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.tab_frag1_layout1,
                container, false);
        TextView tv = (TextView)view.findViewById(R.id.tv1); 
    //  Button playsong = (Button)view.findViewById(R.id.play);
        tv.setText("Android introduced fragments in Android 3.0 (API level 11), " +
                "primarily to support more dynamic and flexible UI designs on large screens, such as tablets. " +
                "Because a tablet's screen is much larger " +
                "than that of a handset, there's more room to combine and interchange UI components.");

        //myStartPreview();
        // myStopPreview();
//      playsong.setOnClickListener(new View.OnClickListener() {
//          
//          public void onClick(View arg0) {
//              // TODO Auto-generated method stub
//              
//              
//              
//              Log.e("","Start==hh"+str);
//          }
//
//          
//      });

        return view;
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        Log.e("","StartAttach=="+str);

    }
//  @Override
     public void myStartPreview( View view) {

         Log.e("","StartPreview=="+str);

     }

     public void myStopPreview(View view){
         Log.e("","StopPreview=="+str); 
     }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Log.e("","StartCreated=="+str);

    }

    @Override
    public void onStart(){
        super.onStart();
        Log.e("","Start=="+str);
    }
    @Override
    public void onResume(){
        super.onResume();
        Log.e("","Resume=="+str);

    }

    @Override
    public void onPause(){
        super.onPause();
        Log.e("","Pause=="+str);

    }
    @Override
    public void onDestroy(){
        super.onDestroy();
        Log.e("","Destroy=="+str);

    }
}

这是我使用的概念的示例代码......

1 个答案:

答案 0 :(得分:0)

PagerAdapter有一个类似的onPageChangeListener。 当页面发生变化时,用它来听,而不是播放你的音频。