티스토리 뷰

반응형

정말 간단한 것을..모르니 엄청난 삽질을..

1. Music Library 사용 기능 추가
Package.appxmanifest -> Capabilities -> Music Library 체크



2. MainPage.xaml
MediaPlayer 추가

<Page
    x:Class="KMusic.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:KMusic"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid x:Name="ContentRoot" Background="{StaticResource ApplicationPageBackgroundBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!-- Back button and page title -->
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="backButton" Style="{StaticResource BackButtonStyle}"/>
            <TextBlock x:Name="pageTitle" Grid.Column="1" Text="KMusic player" Style="{StaticResource PageHeaderTextStyle}"/>
        </Grid>
        <StackPanel Grid.Row="1">
            <StackPanel Orientation="Horizontal">
                <Button x:Name="bAdd" Content="Add Playlist"/>
                <Button x:Name="bPlay" Content="Play Playlist"/>
            </StackPanel>
            <ListBox x:Name="lbPlaylist"/>
            <MediaPlayer x:Name="mp" AutoPlay="True" IsLooping="True"/>
        </StackPanel>
    </Grid>
</Page>

3. MainPage.xaml.cs 

//이벤트 연결

        public MainPage()
        {
            this.InitializeComponent();

            mp.Stop();

            bAdd.Click += bAdd_Click;
            bPlay.Click += bPlay_Click;
        }

//버튼 구현
        async void bAdd_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //파일 선택 창 오픈
                FileOpenPicker picker = this.CreateFilePicker(MainPage.audioExtensions);
                //파일이 선택되면
                StorageFile file = await picker.PickSingleFileAsync();
                if (file != null)
                {
                    //파일을 열어서 스트림으로 만든다.
                    //Music Library로 등록되어 있는 폴더는 직접 접근 가능, 그 외의 폴더는 접근 불가
                    IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);
                    //미디어 플레이어에 소스로 입력
                    mp.SetSource(stream, file.ContentType);
                }

            }
            catch (ArgumentException)
            {
                //this.OutputStatus.Text = "No audio files were selected.";
            }
        }

4. 더 자세한 사항은 첨부된 소스 참조

KMusic.zip
다운로드

5. metro background audio c# (consumer preview)
오디오 서비스를 백그라운드로 실행하는 방법에 대한 포스트

http://babaandthepigman.wordpress.com/2012/03/17/metro-background-audio-c-consumer-preview/

6. media-transport-controls-development
오디오 서비스를 백그라운드로 실행하는 이론 문서

media-transport-controls-development.docx
다운로드

반응형
댓글